go to previous page   go to home page   go to next page

Answer:


Return when Found

Unless you enjoy late nights debugging programs that have no hope of ever running, look over those three answers. Two of them (the wrong ones) are very common bugs. They will happen to you. Learn to recognize them.

The program below has more of the search method. The second if statement immediately returns to the caller with the index of the cell in which target was found.

The second return statement is NOT part of the loop body. It is executed only when every cell of the array has been inspected and the target has not been found.


class Searcher
{
  // seek target in the array of strings.
  // return the index where found, or -1 if not found.
  public static int search( String[] array, String target )
  {
     for ( int j=0; j  < array.length; j++ )
       if ( array[j] != null )
         if ( array[j].equals( target ) ) return  ;

     return  ; // Outside of the Loop!
  }
}

class SearchTester
{
  public static void main ( String[] args )
  {
    . . . . . .
    int where = Searcher.search( strArray, "Peoria" );
    . . . . . .
  }
}

QUESTION 20:

You know what to do with those blanks.